'''
----商超管理系统（容器版）----
项目需求：
系统需要用户先登录，再进行操作，其中包含一下功能菜单：
1.显示商品列表
2.增加商品
3.删除商品
4.设置折扣
5.修改价格
6.按价格排序（升序、降序
7.退出
  a.	使用列表嵌套字典的方式保存用户数据（包含用户名，密码，姓名）；
  b.	使用列表嵌套字典的方式保存商品数据（包含编号，名称，价格，折扣）；
  c.	编写用户登录的函数，返回登录结果；
  d.	循环提示菜单，业务完毕时返回主菜单，退出时回到登陆页面；
  e.	将功能菜单中的业务功能各自编写到函数中；
  f.	用户选择不同业务编号时，调用已经写好的各种函数。
'''
# 添加数据
def init_data():
    global informations_list, goods_list, operations
    information1 = {"username": "admin", "password": "123", "name": "管理员"}
    information2 = {"username": "Monica", "password": "123456", "name": "莫尼卡"}
    information3 = {"username": "Kelly", "password": "123456", "name": "凯利"}
    information4 = {"username": "Laura", "password": "123456", "name": "劳拉"}
    information5 = {"username": "Irene", "password": "123456", "name": "艾琳"}
    information6 = {"username": "Alice", "password": "123456", "name": "艾丽丝"}
    informations_list = [information1, information2, information3, information4, information5,information6]
    goods1 = {"No": "1", "goods_name": "牙膏", "price": 8.0, "discount": 0.0}
    goods2 = {"No": "2", "goods_name": "水杯", "price": 3.0, "discount": 0.0}
    goods3 = {"No": "3", "goods_name": "毛巾", "price": 10.0, "discount": 0.0}
    goods4 = {"No": "4", "goods_name": "牙刷", "price": 8.0, "discount": 0.0}
    goods5 = {"No": "5", "goods_name": "纸巾", "price": 20.0, "discount": 0.0}
    goods_list = [goods1, goods2, goods3, goods4, goods5]
    operations = {1: "显示商品列表", 2: "增加商品", 3: "删除商品", 4: "设置折扣", 5: "修改价格",
                  6: "按价格排序（升序、降序）", 7: "退出"}
# 登录验证函数
def login():
    tag=0
    username=input("请输入用户名：")
    pwd=input("请输入密码：")
    for information in informations_list:
        if username==information.get("username") and pwd==information.get("password"):
            tag = 1
            print(f'恭喜你，{information["name"]}登录成功了')
            break
    if tag==1:
        return 1
    else:
        return 0
#显示商品列表函数
def show_productes():
    print("商品列表：")
    print("编号   名称   价格   折扣")
    for goods in goods_list:
        print(tuple(goods.values()))
# 增加商品函数
def add_product():
    No_list = []
    for goods in goods_list:
        No_list.append(int(goods['No']))
    add_goods_No = str(max(No_list) + 1)  # id 自增
    add_goods_name = input('请输入新商品的名字：')
    add_goods_price = float(input('请输入新商品的价格：'))
    add_goods_discount = float(input('请输入新商品的折扣：'))
    new_goods = {'No': add_goods_No, 'name': add_goods_name, 'price': add_goods_price, 'discount': add_goods_discount}  # 构造新商品
    goods_list.append(new_goods)
    show_productes()
# 删除商品函数
def del_product():
    while True:
        del_goods_No = input("请输入要删除商品的编号：")
        del_tag = 0
        for del_goods in goods_list:
            if del_goods_No == del_goods["No"]:
                del_tag = 1
                goods_list.remove(del_goods)
                break
        if del_tag == 1:
            print("删除成功！！！")
            break
        else:
            print("输入的编号不存在，请继续其他操作!!!")
            otheroption=input("退出删除操作请按1,继续删除请按任意键...")
            if otheroption=="1":
                break
# 设置折扣函数
def set_discount():
    while True:
        discount_goods_No = input("请输入设置折扣的商品编号：")
        discount_tag = 0
        for discount_goods in goods_list:
            if discount_goods_No == discount_goods["No"]:
                discount_tag = 1
                discount_goods["discount"] = float(input("请输入折扣："))
                break
        if discount_tag == 1:
            print("折扣设置成功！！！")
            break
        else:
            print("输入的编号不存在，请继续其他操作!!!")
            otheroption = input("退出设置折扣操作请按1,继续设置折扣请按任意键...")
            if otheroption == "1":
                break
# 修改价格函数
def mod_product():
    while True:
        price_goods_No = input("请输入修改价格的商品编号：")
        price_tag=0
        for price_goods in goods_list:
            if price_goods_No == price_goods["No"]:
                price_tag = 1
                print(f"{price_goods['goods_name']}现在的价格是{price_goods['price']}")
                price_goods["price"] = float(input("请输入修改后的价格："))
                break
        if price_tag == 1:
            print("价格修改成功！！！")
            break
        else:
            print("输入的编号不存在，请重新操作！！！")
            otheroption = input("退出修改价格操作请按1,继续修改价格请按任意键...")
            if otheroption == "1":
                break
# 排序函数
def sort_productes():
    show_productes()
    sort_option = input("请输入按价格排序方式（1-降序，任意键-升序）：")
    if sort_option == "1":
        goods_list.sort(key=lambda x: x["price"])
        show_productes()
    else:
        goods_list.sort(key=lambda x: x["price"],reverse=True)
        show_productes()
def print_menu():
    print("系统首页".center(30, '='))
    print("操作选项：")
    for key, value in operations.items():
        print(f'{key}：{value}')

def main():
    init_data()#初始化数据
    tag = login()#验证
    if tag == 1:
        while True:
            print_menu()
            choice = int(input('请输入你的选择：'))
            if choice == 1:
                show_productes()
            elif choice == 2:
                add_product()
            elif choice == 3:
                del_product()
            elif choice == 4:
                set_discount()
            elif choice == 5:
                mod_product()
            elif choice == 6:
                sort_productes()
            elif choice == 7:
                print('正在退出')
                break
            else:
                print('输入有误，请重新输入')
    else:
        print("登录错误！！")

if __name__ == '__main__':
     main()
    # init_data()
    # print(assert())
    # show_productes()
    # add_product()
    # del_product()
    # set_discount()
    # mod_product()
    # sort_productes()
    # print_menu



